package com.jeff.android.lab.module.anim.path;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * @author: duff
 * @date: 2019/1/2
 * @since: 1.0.0
 */
public class PathView extends View {
    private Path mPath;
    private Paint mBezierPaint;
    private Paint mFlagPaint;
    private Paint mFlagTextPaint;

    //起点
    private float mStartPointX;
    private float mStartPointY;
    //终点
    private float mEndPointX;
    private float mEndPointY;
    //控制点
    private float mFlagPointX;
    private float mFlagPointY;

    private float mFlagPoint2X;
    private float mFlagPoint2Y;

    public PathView(Context context) {
        super(context);
        init();
    }

    public PathView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PathView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mBezierPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBezierPaint.setStrokeWidth(8);
        mBezierPaint.setColor(Color.parseColor("#00aff9"));
        mBezierPaint.setStyle(Paint.Style.STROKE);

        mFlagPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mFlagPaint.setStrokeWidth(3);
        mFlagPaint.setStyle(Paint.Style.STROKE);

        mFlagTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mFlagTextPaint.setStyle(Paint.Style.STROKE);
        mFlagTextPaint.setTextSize(20);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        //初始时确定起点、终点和控制点的坐标
        mStartPointX = 0;
        mStartPointY = h / 2;

        mEndPointX = w * 3 / 4;
        mEndPointY = h / 2 - 200;

        mFlagPointX = w / 2;
        mFlagPointY = h / 2 - 300;

        mFlagPoint2X = w * 2 / 3;
        mFlagPoint2Y = h / 2 - 500;

        mPath = new Path();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPath.reset();
        mPath.moveTo(mStartPointX, mStartPointY);
//        mPath.quadTo(mFlagPointX, mFlagPointY, mEndPointX, mEndPointY);
        mPath.cubicTo(mFlagPointX, mFlagPointY, mFlagPoint2X, mFlagPoint2Y, mEndPointX, mEndPointY);

        canvas.drawPoint(mStartPointX, mStartPointY, mFlagPaint);
        canvas.drawText("起点", mStartPointX, mStartPointY, mFlagTextPaint);

        canvas.drawPoint(mEndPointX, mEndPointY, mFlagPaint);
        canvas.drawText("终点", mEndPointX, mEndPointY, mFlagTextPaint);

        canvas.drawPoint(mFlagPointX, mFlagPointY, mFlagPaint);
        canvas.drawText("控制点", mFlagPointX, mFlagPointY, mFlagTextPaint);

        canvas.drawPoint(mFlagPoint2X, mFlagPoint2Y, mFlagPaint);
        canvas.drawText("控制点2", mFlagPoint2X, mFlagPoint2Y, mFlagTextPaint);

//        canvas.drawLine(mStartPointX, mStartPointY, mFlagPointX, mFlagPointY, mFlagPaint);
//        canvas.drawLine(mEndPointX, mEndPointY, mFlagPointX, mFlagPointY, mFlagPaint);

        canvas.drawLine(mStartPointX, mStartPointY, mFlagPointX, mFlagPointY, mFlagPaint);
        canvas.drawLine(mFlagPointX, mFlagPointY, mFlagPoint2X, mFlagPoint2Y, mFlagPaint);
        canvas.drawLine(mEndPointX, mEndPointY, mFlagPoint2X, mFlagPoint2Y, mFlagPaint);

        canvas.drawPath(mPath, mBezierPaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                mFlagPointX = event.getX();
                mFlagPointY = event.getY();
                invalidate();
                break;
        }
        return true;
    }

}
